/*
 * @FilePath: /tads/server/app/business/apis/bus.go
 * @Author: wnina0x0
 * @Date: 2025-04-15 14:55:31
 * @Description:
 *
 * 版权信息 : 2025 by wnina0x0, All Rights Reserved.
 */
/*
 * @FilePath: /tads/server/app/business/apis/bus.go
 * @Author: wnina0x0
 * @Date: 2025-04-15 14:55:31
 * @Description:
 *
 * 版权信息 : 2025 by wnina0x0, All Rights Reserved.
 */
package apis

import (
	"fmt"
	"net/http"
	"tads/server/app/business/models"
	"tads/server/app/business/service"
	"tads/server/app/business/service/dto"
	"tads/server/core/sdk/api"

	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

type Bus struct {
	api.Api
}

func (e Bus) GetList(c *gin.Context) {
	s := service.Bus{}
	err := e.MakeContext(c).MakePostgres().MakeService(&s.Service).Errors
	if err != nil {
		e.Logger.Errorf("总线获取错误：%s\n", err.Error())
		c.JSON(http.StatusOK, gin.H{"code": e.Code, "msg": fmt.Sprintf("总线获取错误：%s\n", err.Error())})
		return
	}
	res := []models.BusType{}
	if err = s.GetBusList(&res); err != nil {
		e.Logger.Errorf("总线获取错误：%s\n", err.Error())
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprintf("总线获取错误：%s\n", err.Error())})
		return
	}
	e.Logger.Info("总线获取成功")
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "总线获取成功", "data": res})
	return
}

func (e Bus) Get(c *gin.Context) {
	req := dto.BusGetRequests{}
	s := service.Bus{}
	err := e.MakeContext(c).MakePostgres().Bind(&req, binding.Query).MakeService(&s.Service).Errors
	if err != nil {
		e.Logger.Errorf("总线获取错误：%s\n", err.Error())
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprintf("总线获取错误：%s\n", err.Error())})
		return
	}

	response, err := s.GetBus(&req)
	if err != nil {
		e.Logger.Errorf("总线获取错误：%s\n", err.Error())
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprintf("总线获取错误：%s\n", err.Error())})
		return
	}
	e.Logger.Info("总线获取成功")
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "总线获取成功", "data": response})
	return
}

func (e Bus) Inset(c *gin.Context) {
	req := dto.BusInsetRequests{}
	s := service.Bus{}
	err := e.MakeContext(c).MakePostgres().Bind(&req, binding.JSON).MakeService(&s.Service).Errors
	if err != nil {
		e.Logger.Errorf("总线插入数据错误：%s\n", err.Error())
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprintf("总线插入数据错误：%s\n", err.Error())})
		return
	}
	if req.BusTypeDesc == "" || req.BusTypeName == "" {
		e.Logger.Errorf("插入失败:%s\n", fmt.Errorf("缺少参数"))
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprintf("请求参数错误: %s!\n", fmt.Errorf("缺少参数"))})
		return
	}
	if err = s.Inset(&req); err != nil {
		e.Logger.Errorf("总线插入数据错误：%s\n", err.Error())
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprintf("总线插入数据错误：%s\n", err.Error())})
		return
	}
	e.Logger.Info("总线插入成功")
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "总线插入成功"})
	return
}

func (e Bus) Update(c *gin.Context) {
	req := dto.BusUpdateRequests{}
	s := service.Bus{}
	err := e.MakeContext(c).MakePostgres().Bind(&req, binding.JSON).MakeService(&s.Service).Errors
	if err != nil {
		e.Logger.Errorf("总线更新数据错误：%s\n", err.Error())
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprintf("总线更新数据错误：%s\n", err.Error())})
		return
	}
	if err = s.Update(&req); err != nil {
		e.Logger.Errorf("总线更新数据错误：%s\n", err.Error())
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprintf("总线更新数据错误：%s\n", err.Error())})
		return
	}
	e.Logger.Info("总线修改成功")
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "总线修改成功"})
	return
}

func (e Bus) Status(c *gin.Context) {
	req := dto.BusStatusRequests{}
	s := service.Bus{}
	err := e.MakeContext(c).MakePostgres().Bind(&req, binding.JSON).MakeService(&s.Service).Errors
	if err != nil {
		e.Logger.Errorf("总线状态修改失败：%s\n", err.Error())
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprintf("总线状态修改失败：%s\n", err.Error())})
		return
	}
	if err = s.Status(&req); err != nil {
		e.Logger.Errorf("总线状态修改失败：%s\n", err.Error())
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": fmt.Sprintf("总线状态修改失败：%s\n", err.Error())})
		return
	}
	e.Logger.Info("总线状态修改成功")
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "总线状态修改成功"})
	return

}
